package com.google.project.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.project.client.Service.UsersService;
import com.google.project.server.entity.Users;
import com.google.project.shared.ProjectStaffDTO;
import com.google.project.shared.UsersDTO;

@SuppressWarnings("serial")
public class UsersServiceImpl extends RemoteServiceServlet implements
		UsersService {

	@Override
	public UsersDTO addUser(UsersDTO userDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Users user = null;
		try {
			user = new Users(userDTO);
			pm.makePersistent(user);
		}catch(Exception ex){
			ex.getMessage();
		}
		finally {
			pm.close();
		}
		return user.toDTO();
	}

	@Override
	public void deleteUser(String id) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Users user = pm.getObjectById(Users.class, id);
			if (user != null) {
				pm.deletePersistent(user);
			}
		} finally {
			pm.close();
		}
	}

	@Override
	public UsersDTO getUserById(String userId) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Users userInfo;
		try {
			userInfo = pm.getObjectById(Users.class, userId);

		} finally {
			pm.close();
		}

		return userInfo.toDTO();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsersDTO> getUserList() {
		List<UsersDTO> userList = new ArrayList<UsersDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Users.class);
		try {
			List<Users> list = (List<Users>) q.execute();
			for (Users user : list) {
				userList.add(user.toDTO());
			}
		} catch (Exception e) {
		   e.getMessage();
		} finally {
			pm.close();
		}
		return userList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsersDTO> searchUserList() {
		// TODO Auto-generated method stub
		List<UsersDTO> userList = new ArrayList<UsersDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String WhereString = "";
		// Se them dieu kien vao cho nay de tim theo dieu kien
		Query q;
		if (WhereString != "") {
			q = pm.newQuery(Users.class, WhereString);
		} else {
			q = pm.newQuery(Users.class);
		}

		try {
			List<Users> list = (List<Users>) q.execute();
			for (Users user : list) {
				userList.add(user.toDTO());
			}
		} catch (Exception e) {

		} finally {
			pm.close();
		}
		return userList;
	}

	@Override
	public void updateUser(UsersDTO userDTO) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Users user = null;
		try {
			user = pm.getObjectById(Users.class, userDTO.getUserId());
			//userDTO=user.toDTO();
			user.updateFromDTO(userDTO);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}
	@Override
	public void updateAccount(UsersDTO userDTO) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Users user = null;
		try {
			user = pm.getObjectById(Users.class, userDTO.getUserId());
			//userDTO=user.toDTO();
			user.updateAcc(userDTO);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}	
	@SuppressWarnings("unchecked")
	@Override
	public List<UsersDTO> searchUserList2(Long userGroup, String status,
			String userCode) {
		List<UsersDTO> userList = new ArrayList<UsersDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String WhereString = "";
		// status
		try {
			// userCode
			if (userCode != null && !userCode.equalsIgnoreCase("")) {
				WhereString += "  userCode ==  \"" + userCode + "\"";
			}
			if (status != null && !status.equalsIgnoreCase("")) {
				if (WhereString != "") {
					WhereString += " && status ==  \"" + status + "\"";
				} else if (WhereString.equalsIgnoreCase("")) {
					WhereString += " status ==  \"" + status + "\"";
				}
			}
			if (userGroup != 0) {
				if (WhereString != "") {
					WhereString += " && userGroup ==  " + userGroup;
				} else if (WhereString.equalsIgnoreCase("")) {
					WhereString += " userGroup ==  " + userGroup;
				}
			}
		} catch (Exception e) {

		}

		Query q;
		if (WhereString != "") {
			q = pm.newQuery(Users.class, WhereString);
		} else {
			q = pm.newQuery(Users.class);
		}

		try {
			List<Users> list = (List<Users>) q.execute();
			for (Users user : list) {
				userList.add(user.toDTO());
			}
		} catch (Exception e) {
		} finally {
			pm.close();
		}
		return userList;
	}

	@Override
	public void deleteUserList(List<UsersDTO> list) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			for (UsersDTO usersDTO : list) {
				Users user = pm
						.getObjectById(Users.class, usersDTO.getUserId());
				if (user != null) {
					pm.deletePersistent(user);
				}
			}

		} finally {
			pm.close();
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsersDTO> searchUserList(String userName, String currentProjID,
			String specID, int exp) {
		List<UsersDTO> userList = new ArrayList<UsersDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String WhereString = "";
		// status
		try {
			// userCode
			if (userName != null && !userName.equalsIgnoreCase("")) {
				WhereString += "  userName ==  \"" + userName + "\"";
			}
			if (currentProjID != null && !currentProjID.equalsIgnoreCase("")
					&& !currentProjID.equalsIgnoreCase("0")) {
				if (WhereString != "") {
					WhereString += " && currentProjID ==  \"" + currentProjID
							+ "\"";
				} else if (WhereString.equalsIgnoreCase("")) {
					WhereString += " currentProjID ==  \"" + currentProjID
							+ "\"";
				}
			}
			if (specID != null && !specID.equalsIgnoreCase("")
					&& !specID.equalsIgnoreCase("0")) {
				if (WhereString != "") {
					WhereString += " && specializeID ==  \"" + specID + "\"";
				} else if (WhereString.equalsIgnoreCase("")) {
					WhereString += " specializeID ==  \"" + specID + "\"";
				}
			}
			if (exp != 0) {
				if (WhereString != "") {
					WhereString += " && experient ==  " + exp;
				} else if (WhereString.equalsIgnoreCase("")) {
					WhereString += " experient ==  " + exp;
				}
			}
		} catch (Exception e) {

		}

		Query q;
		if (WhereString != "") {
			q = pm.newQuery(Users.class, WhereString);
		} else {
			q = pm.newQuery(Users.class);
		}

		try {
			List<Users> list = (List<Users>) q.execute();
			for (Users user : list) {
				userList.add(user.toDTO());
			}
		} catch (Exception e) {
		} finally {
			pm.close();
		}
		return userList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsersDTO> getUserListByCurrentPJId() {
		List<UsersDTO> userList = new ArrayList<UsersDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String whereString = "currentProjID == null";
		// status
		Query q = pm.newQuery(Users.class, whereString);
		try {
			List<Users> list = (List<Users>) q.execute();
			for (Users user : list) {
				userList.add(user.toDTO());
			}
		} catch (Exception e) {
		} finally {
			pm.close();
		}
		return userList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsersDTO> searchUserList(String fullName, String userCode, String specializeName) {
		// TODO Auto-generated method stub
		List<UsersDTO> userList = new ArrayList<UsersDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String whereString = "";
//		String whereString = " currentProjID == null";
		// status
		try {
			if (fullName != null && !fullName.equalsIgnoreCase("")) {
				if(whereString != "") {
					whereString += " && fullName ==  \"" + fullName + "\"";
				} else {
					whereString += " fullName ==  \"" + fullName + "\"";
				}
			}
			if (userCode != null && !userCode.equalsIgnoreCase("")) {
				if(whereString != "") {
					whereString += " && userCode ==  \"" + userCode + "\"";
				} else {
					whereString += " userCode ==  \"" + userCode + "\"";
				}
			}
			if (specializeName != null && !specializeName.equalsIgnoreCase("")) {
				if(whereString != "") {
					whereString += " && specializeName ==  \"" + specializeName + "\"";
				} else {
					whereString += " specializeName ==  \"" + specializeName + "\"";
				}
			}
		} catch (Exception e) {
			log("UsersServiceImpl.searchUserList: " + e.getMessage());
		}
		Query q = pm.newQuery(Users.class, whereString);

		try {
			List<Users> list = (List<Users>) q.execute();
			for (Users user : list) {
				userList.add(user.toDTO());
			}
		} catch (Exception e) {
			log("UsersServiceImpl.searchUserList: " + e.getMessage());
		} finally {
			pm.close();
		}
		return userList;
	}

	@Override
	public List<UsersDTO> getUserListExceptById(List<ProjectStaffDTO> projectStaffDTOList) {
		List<UsersDTO> userAllList = getUserList();
		for(int i = 0; i < projectStaffDTOList.size(); i++) {
			for(int j = 0; j < userAllList.size(); j++) {
				if(projectStaffDTOList.get(i).getUserId().equals(userAllList.get(j).getUserId())) {
					userAllList.remove(j);
				}
			}
		}
		
		return userAllList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsersDTO> getUserListByStatus(String status) {
		List<UsersDTO> userList = new ArrayList<UsersDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String whereString = "status == \"" + status + "\"";
		Query q = pm.newQuery(Users.class, whereString);
		try {
			List<Users> list = (List<Users>) q.execute();
			for (Users user : list) {
				userList.add(user.toDTO());
			}
		} catch (Exception e) {
		} finally {
			pm.close();
		}
		return userList;
	}
	
	@Override
	public List<UsersDTO> getInfoByUserCode(String userCode) {
		List<UsersDTO> userList = new ArrayList<UsersDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String whereString = "userCode == \"" + userCode + "\"";
		Query q = pm.newQuery(Users.class, whereString);
		try {
			List<Users> list = (List<Users>) q.execute();
			for (Users user : list) {
				userList.add(user.toDTO());
			}
		} catch (Exception e) {
		} finally {
			pm.close();
		}
		return userList;
	}

}
